package com.wu.foodsafetyplayer.app.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.google.gson.JsonSyntaxException;
import com.wu.foodsafetyplayer.Application;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.AddDeviceActivity;
import com.wu.foodsafetyplayer.app.AppMainActivity;
import com.wu.foodsafetyplayer.app.bean.UserEntity;
import com.wu.foodsafetyplayer.http.CommonOkHttpClient;
import com.wu.foodsafetyplayer.utils.SpUtil;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 菜单选择弹窗
 */
public class DevicePopWindow extends PopupWindow {

    private TextView btnAddDevice;
    private TextView btnLoginOut;

    public DevicePopWindow(FragmentActivity mContext){
        super(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        View inflate = LayoutInflater.from(Application.getAppContext()).inflate(R.layout.pop_window_device, null);
        setContentView(inflate);
        setAnimationStyle(R.style.popupwindow_style);

        btnAddDevice = inflate.findViewById(R.id.btn_add_device);
        btnLoginOut = inflate.findViewById(R.id.btn_login_out);

        btnAddDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mContext.startActivity(new Intent(mContext, AddDeviceActivity.class));
            }
        });

        btnLoginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginOut(mContext);
            }
        });
    }

    private void loginOut(FragmentActivity mContext){
        CommonOkHttpClient.FUNC_TASK.execute(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();

                Request request = new Request.Builder()
                        .url("http://daxun.tpddns.cn:81/LogoutService?token=" + SpUtil.INSTANCE.getUserInfo().getToken())
                        .method("GET", null)
                        .addHeader("content-type", "form-data")
                        .addHeader("Cookie", "IDHTTPSESSIONID=r4EN2qalGkkbZUS")
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String res2 = response.body().string();
                    if (!TextUtils.isEmpty(res2)) {
                        dismiss();
                        mContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext,"退出登录成功!",Toast.LENGTH_SHORT).show();
                            }
                        });
                        //退出登录，清空登录缓存信息
                        UserEntity userEntity = new UserEntity();
                        userEntity.setResult(-1);
                        userEntity.setToken("");
                        userEntity.setIsnoLogin(true);
                        SpUtil.INSTANCE.saveUserInfo(userEntity);
                        mContext.startActivity(new Intent(mContext, AppMainActivity.class));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassCastException e) {
                    e.printStackTrace();
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
